package com.example.study_backstage.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.study_backstage.constant.CommonConstant;
import com.example.study_backstage.entity.SysUser;
import com.example.study_backstage.entity.dto.LoginUser;
import com.example.study_backstage.service.SysUserService;
import com.example.study_backstage.utils.JwtUtil;
import com.example.study_backstage.utils.RedisUtil;
import com.example.study_backstage.utils.Result;
import com.example.study_backstage.utils.oConvertUtils;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.apache.catalina.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;

/**
 * @author lxw
 * @version 1.0
 * @data 2023/3/22
 */
@RestController
@RequestMapping("/sys")
public class LoginController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/login")
    public Result<JSONObject> login(@RequestBody LoginUser loginUser){
        Result<JSONObject> result = new Result<>();
        //查询用户是否存在
        SysUser user = sysUserService.getUser(loginUser.getUserName());
        if (oConvertUtils.isEmpty(user)){
            result.error500("账号不存在!");
            return result;
        }
        if (!loginUser.getPassWord().equals(user.getPassWord())){//密码判断
            result.error500("用户名或密码错误!");
            return result;
        }
        userInfo(user,result);
        return result;
    }

    private Result<JSONObject> userInfo(SysUser user, Result<JSONObject> result) {
        String userName = user.getUserName();
        String passWord = user.getPassWord();
        JSONObject obj = new JSONObject(new LinkedHashMap<>());
        //1.生成token
        String token = JwtUtil.sign(userName, passWord);
        // 设置token缓存有效时间
        redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
        redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
        obj.put("token", token);

        //BeanCopier beanCopier = BeanCopier.create(User.class, LoginUser.class, false);
        //LoginUser loginUser = new LoginUser();
        //beanCopier.copy(user,loginUser,null);

        // 设置用户缓存有效时间
        redisUtil.set(CommonConstant.SYS_USERS_CACHE + "::" + userName,user);
        redisUtil.expire(CommonConstant.SYS_USERS_CACHE + "::" + userName, JwtUtil.EXPIRE_TIME * 2 / 1000);

        //3.设置登录用户信息
        obj.put("userInfo", user);
        result.setResult(obj);
        result.success("登录成功");
        return result;
    }
}
